var _CONFIG = {
    JQXGRID: '#jqx_grid_view',
    CATEGORYS:null,
    CATEFILLTER:'',
    BINDING_URI: base_url + 'cms/cp/contact/bindingus/',
    EDITPANEL_URI:base_url + 'cms/cp/contact/editpanel/',
    COMMIT_URI:base_url + 'cms/cp/contact/oncommit/',
    DELETE_URI:base_url + 'cms/cp/contact/ondelete/'
};
var _CP = (function() {
    var datafields = [
        {name: 'Id', type: 'int'},
        {name: 'Name'},
        {name: 'Mail'},
        {name: 'Phone'},
		{name: 'Message'},
        {name: 'Insert', type: 'date'}
    ];
    var columns = [
        {text: 'Name'       , dataField: 'Name'},
		{text: 'Mail'       , dataField: 'Mail'},
		{text: 'Phone'       , dataField: 'Phone'},
		{text: 'Message'       , dataField: 'Message'},
        {text: 'Created'        , dataField: 'Insert'      , width: 120, filterable: true,filtertype: 'date', cellsformat: 'yyyy-MM-dd HH:mm:ss', editable: false},
        
    ];
    var oTable;
    var that = this;
    var source,dataAdapter;
    return {
        onCommit: function(Params, Id,callback) {
            /* Commit data lên vào DB, Id>0 là Update, Id=0 hoặc ko có là Insert */
            backend({
                url: _CONFIG.COMMIT_URI,
                data: {
                    Id: Id,
                    Params: Params
                },
                callback: function(rsdata) {
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {sticky: true, theme: 'growl-error', header: 'Error!'});
                    } else {
                        $.jGrowl(rsdata.message);
                        if (typeof callback == 'function') {
                            callback();
                        }
                    }
                }
            }).call();
        },
        creategrid  :   function(){
            source = {
                datatype: "json", type: "POST",
                datafields: datafields,
                url: _CONFIG.BINDING_URI,
                filter: function() {
                    $(_CONFIG.JQXGRID).jqxGrid('updatebounddata', 'filter');
                },
                sort: function() {
                    $(_CONFIG.JQXGRID).jqxGrid('updatebounddata');
                },
                root: 'rows',
                beforeprocessing: function(data) {
                    
                }
            };
            dataAdapter = new $.jqx.dataAdapter(source, {
                loadError: function(xhr, status, error) {
                    bckdialog({type: 'error'}).open("<b>Status</b>:" + xhr.status + "<br/><b>ThrownError</b>:" + error + "<br/>", 'Error Message!');
                }
            });
            
            $(_CONFIG.JQXGRID).jqxGrid({
                width: '100%', //autoheight:true,
                //height: '100%',
                source: dataAdapter,
                theme: theme, sortable: true,
                filterable: true,
                autoshowfiltericon: true,
                showfilterrow: true,
                editable: false,
                //rowsheight:34,
                showemptyrow: true,
                columns: columns,
                selectionmode: 'singlecell',
                editmode: 'dblclick',
                virtualmode: true,
                //groupable: true,
                rendergridrows: function(){ return dataAdapter.records; },
                pageable: true,
                pagesize: 100,
                pagesizeoptions: ['100', '200', '500', '1000'],
                ready: function() {
                    var $refresh = $('<div type="button"  title="Refresh" style="margin-left: 0px;padding:0 4px; float: left; cursor: pointer;height:15px;"><img src="/libraries/images/16/view_refresh.png"/></div>');
                    var $option = $('<div type="button"  title="Option" style="margin-left: 0px;padding:0 4px; float: left; cursor: pointer;height:15px;"><img src="/libraries/images/16/option.gif"/></div>');
                    $refresh.click(function(){
                        _CP.onRefresh();
                    });
                    $option.prependTo($(_CONFIG.JQXGRID+' .jqx-grid-pager>div'));
                    $refresh.prependTo($(_CONFIG.JQXGRID+' .jqx-grid-pager>div'));
                    
                },
                handlekeyboardnavigation: function(event)
                {
                    var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
                    if (key == 27) {
                        that.cancel=true;
                    }
                },

            });
        },
        onInit  :   function(){
            _CP.creategrid();
        },
        onRefresh:function(){
            //oTable.fnDraw(false);
            $(_CONFIG.JQXGRID).jqxGrid('updatebounddata');
        },
        onFillter: function(datafield, filtervalue) {
            if (filtervalue == '') {
                $(_CONFIG.JQXGRID).jqxGrid('removefilter', datafield, true);
                return;
            }
            var filtertype = 'stringfilter';
            var filtergroup = new $.jqx.filter();
            var filter_or_operator = 1;
            var filtercondition = 'contains';
            var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
            filtergroup.addfilter(filter_or_operator, filter);
            
            // add the filters.
            $(_CONFIG.JQXGRID).jqxGrid('addfilter', datafield, filtergroup);
            // apply the filters.
            $(_CONFIG.JQXGRID).jqxGrid('applyfilters');
        }
    };
}());

$(document).ready(function(){
    _CP.onInit();
});
